package com.nut.article.api.biz;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nut.article.api.req.SaveSortReq;
import com.nut.article.common.Result;
import com.nut.article.entity.Article;
import com.nut.article.entity.ArticleVo;
import com.nut.article.entity.Tag;
import com.nut.article.service.ArticleService;
import com.nut.article.service.TagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private TagService tagService;

    @GetMapping(value = "list")
    public Result<IPage<Article>> list(@RequestParam(required = false) String name,
                                       @RequestParam(required = false) Integer categoryId,
                                       @RequestParam(required = false) Integer status,
                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return articleService.list(name, categoryId, status, pageNo, pageSize);
    }

    @GetMapping(value = "getByTag")
    public Result<IPage<ArticleVo>> getByTag(@RequestParam Integer tagId,
                                             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return articleService.getByTag(tagId, pageNo, pageSize);
    }

    @GetMapping(value = "index")
    public Result<Map<String, Object>> index() {

        List<ArticleVo> news = articleService.newList();
        List<ArticleVo> hots = articleService.hotList();
        List<ArticleVo> sides = articleService.sideList();
        List<ArticleVo> tops = articleService.topList();
        List<Tag> tags = tagService.list();

        HashMap<String, Object> map = new HashMap<>();
        //获取最新文章
        map.put("new", news);
        //获取热门排行榜  头部5个
        map.put("hot", hots);
        //获取推荐排行榜  侧边栏
        map.put("side", sides);
        //获取置顶文章
        map.put("top", tops);
        //获取所有标签
        map.put("tag", tags);

        return Result.ok(map);
    }

    //查找文章
    @GetMapping(value = "search")
    public Result<List<ArticleVo>> search(@RequestParam String keywords) {
        List<ArticleVo> list = articleService.searchList(keywords);
        return Result.ok(list);
    }

    //最新文章
    @GetMapping(value = "news")
    public Result<List<ArticleVo>> news() {
        List<ArticleVo> news = articleService.newList();
        return Result.ok(news);
    }

    //热门板块
    @GetMapping(value = "hots")
    public Result<List<ArticleVo>> hots() {
        List<ArticleVo> hots = articleService.hotList();
        return Result.ok(hots);
    }

    //侧栏板块
    @GetMapping(value = "sides")
    public Result<List<ArticleVo>> sides() {
        List<ArticleVo> sides = articleService.sideList();
        return Result.ok(sides);
    }

    //置顶推荐
    @GetMapping(value = "tops")
    public Result<List<ArticleVo>> tops() {
        List<ArticleVo> tops = articleService.topList();
        return Result.ok(tops);
    }

    //保存排序规则
    @PostMapping(value = "saveSort")
    public Result saveSort(@RequestBody SaveSortReq saveSortReq) {
        return articleService.saveSort(saveSortReq);
    }

    @GetMapping(value = "/{id}")
    public Result<Article> detail(@PathVariable Integer id) {
        return articleService.getById(id);
    }

    @GetMapping(value = "/downloadInfo")
    public Result<String> downloadInfo(@RequestParam Integer id, @RequestParam String code) {
        return articleService.getDownloadInfoById(id, code);
    }

    @PostMapping
    public Result add(@RequestBody Article article) {
        return articleService.add(article);
    }

    @PutMapping
    public Result update(@RequestBody Article article) {
        return articleService.updateById(article);
    }

    @DeleteMapping
    public Result delete(@RequestBody Set<Integer> ids) {
        return articleService.deleteByIds(ids);
    }

    @GetMapping(value = "/setCode")
    public Result setCode(@RequestParam String code) {
        return articleService.setCode(code);
    }

    @GetMapping(value = "/getCode")
    public Result getCode() {
        return articleService.getCode();
    }

}
